#include "stdafx.h"
#include "HandHistoryParser.h"
#include <fstream>
#include <iostream>

list<Hand> HandHistoryParser::Parse(std::string filePath)
{
	Hand h;
	list<Hand> handList;
	string line;
	list<string> lines;
	ifstream fStream(filePath.c_str());		

	while (fStream) {					
		getline(fStream, line);		// using getline for C++ style strings		
 		line.append("\r\n");			// getline strips newline characters (\r\n is for windows) so append it back
		if (line != "\r\n")
		{
			if (line.find("said, \"") != string.npos 
				|| line.find("is sitting out") != string.npos
				|| line.find(" has returned") != string.npos
				|| line.find(" will be allowed to play after the button") != string.npos
				|| line.find(": sits out") != string.npos
				|| line.find(" leaves the table") != string.npos)	// filter out chat
				continue;
			lines.push_back(line);			
		}
		else
		{
			if (!lines.empty())
			{
				h = hp_.Parse(lines);
				cout << h.Id() << endl;
				handList.push_front(h);
				lines.clear();
			}
		}			
	}

	// if no newline after the last hand of the file
	if (!lines.empty())
	{
		h = hp_.Parse(lines);
		handList.push_front(h);
		lines.clear();
	}

	fStream.close();
	return handList;
}